#!/usr/bin/env python
import sys
import os

sys.path.append("tools/build")

import build

#config = "Release"
config = "Debug"

srcDir = "src"
buildDir = "bin/%s" % (config)
cppFlags = ["-Iinclude","-Wall"]
if config == "Debug":
    cppFlags[0] += " -g"
elif config == "Release":
    cppFlags[0] += " -O3"

coreLdFlags = "-lGLEW -lGL -lGLU -lCg -lCgGL -lSDL -lIL -lode"
logicLdFlags = coreLdFlags
#logicLdFlags.append(buildDir + "/core.a")

coreMod = build.Mod(srcDir,"core",buildDir,cppFlags)
coreDrawMod = build.Mod(srcDir,"core/draw",buildDir,cppFlags)
#coreDynaMod = build.Mod(srcDir,"core/dyna",buildDir,cppFlags)
coreCollMod = build.Mod(srcDir,"core/coll",buildDir,cppFlags)
#coreScgrMod = build.Mod(srcDir,"core/scgr",buildDir,cppFlags)
coreEvntMod = build.Mod(srcDir,"core/evnt",buildDir,cppFlags)

coreMods = [ coreMod, coreDrawMod, coreCollMod, coreEvntMod ]

coreLib = build.Library(coreMods, buildDir, "core.a", "")


def buildCore():
    coreLib.build()

def cleanCore():
    coreScgrMod.clean()
    coreCollMod.clean()
    coreDrawMod.clean()
    coreDynaMod.clean()
    coreMod.clean()

logicMod = build.Mod(srcDir,"logic",buildDir,cppFlags)
logicLib = build.Library([logicMod], buildDir, "logic.a", "")

def buildLogic():
    buildCore()
    logicLib.build()

def cleanLogic():
    logicMod.clean()
    
gameMod = build.Mod(srcDir, "game", buildDir, cppFlags)
gameProg = build.Program([gameMod], buildDir, "bloody", "%s/logic.a %s/core.a %s" % ( buildDir, buildDir, coreLdFlags))
    
def buildGame():
    buildCore()
    buildLogic()
    gameProg.build()

def cleanGame():
    gameMod.clean()

def pkgConfig(lib, flags):
    fp = os.popen("pkg-config %s %s" % (lib,flags))
    ret = fp.read()
    fp.close()
    return ret.strip()



editorCppFlags = list(cppFlags)
editorCppFlags.append(pkgConfig("gtkglextmm-1.2 libglademm-2.4","--cflags"))
editorLdFlags = coreLdFlags + " " + pkgConfig("gtkglextmm-1.2 libglademm-2.4","--libs")
editorMod = build.Mod(srcDir, "editor", buildDir, editorCppFlags)
editorProg = build.Program([editorMod], buildDir, "bloody-ed", "%s/logic.a %s/core.a %s" %(buildDir, buildDir, editorLdFlags))

def buildEditor():
    buildCore()
    buildLogic()
    editorProg.build()

def cleanEditor():
    editorMod.clean()




#
#
#
#===========================================================================================
#
#
#
#    Open test definition file (target)
#    file syntax:
#      <key> = <value>
#
#    valid keys = srcDir, reqLib, dest, name
#
#    key values:
#    srcDir - (string) path to source directory
#    reqLib - (list) of librart names (for example:"core, logic" )
#    dest - unused
#    name - (string) name of test (exec file name)
#
#
#
#
#============================================================================================
#
#
#





def openTest(target):
    fp = open(target)
    lines = fp.readlines()
    for line in lines:
        line = line.strip()
        tkns = line.split("=")
        if tkns[0] == "srcDir":
            srcDir = tkns[1]
        elif tkns[0] == "reqLib":
            reqLib = tkns[1]
        elif tkns[0] == "dest":
            dest = tkns[1]
        elif tkns[0] == "name":
            name = tkns[1]
    libs = reqLib.split(",")
    ldFlags = "-lGL -lGLU -lSDL -lIL "
    for l in libs:
        ldFlags += "%s/%s.a" % (buildDir, l)
    mod = build.Mod(srcDir, name, buildDir, cppFlags)
    prog = build.Program([mod], buildDir, "test."+name, ldFlags)

    return [mod, prog]

    

def buildTest(target):
    [mod, prog] = openTest(target)
    prog.build()

def cleanTest(target):
    [mod, prog] = openTest(target)
    #prog.clean()
    mod.clean()







def buildTarget(target):
    try:
        if target == "core":
    	    buildCore()
	elif target == "logic":
	    buildLogic()
        elif target == "editor":
    	    buildEditor()
	elif target == "game":
	    buildGame()
        elif target == "all":
            buildCore()
            buildLogic()
            buildGame()
            buildEditor()
        else:
            buildTest(target)
    except build.NotCompiledException,e:
        print e.what()
	print "Building error :-/"

def cleanTarget(target):
    if target == "core":
        cleanCore()
    elif target == "logic":
        cleanLogic()
    elif target == "editor":
        cleanEditor()
    elif target == "game":
        cleanGame()
    elif target == "all":
        cleanCore()
        cleanLogic()
        cleanGame()
        cleanEditor()
    else:
        cleanTest(target)

def etags():
    src = ""
    for root, dirs, files in os.walk("src"):
        for f in files:
            if f.rfind(".cpp") + 4 == len(f):
                src += root + "/" + f + " "
    for root, dirs, files in os.walk("include"):
        for f in files:
            if f.rfind(".h") + 2 == len(f):
                src +=  root + "/" + f + " "
    print "etags",src
    os.system("etags " + src)


if __name__ == "__main__":
    
    if len(sys.argv) < 2:
	target = "core"
    else:
        target = sys.argv[1]

    if target == "etags":
        etags()
        
    
    action = "build"
    if len(sys.argv) > 2:
        action = sys.argv[2]
    if action == "build":
        buildTarget(target)
    elif action == "clean":
        cleanTarget(target)
        
    else:
        print "Unknown action:",target
        

